<?php 
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id$
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2010 Forrest Chamberlain. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/**
 * In this page, we display:
 *  - all existing wishlists -- ordered from newest to oldest
 *  OR
 *  - the contents of a wishlist, if a wishlist_id is present
 */
mm_showMyFileName( __FILE__ );

if ($perm->check( "admin,storeadmin" ))
{
	require_once( CLASSPATH . 'ps_wishlist.php' );
	require_once( CLASSPATH . "pageNavigation.class.php" );
	require_once( CLASSPATH . "htmlTools.class.php" );


	$wishlist_id = JRequest::getInt('wishlist_id');
	$func = JRequest::getInt('func');
	$orderby = 'created DESC';

	$result = false;

	if (!isset( $wishlist_id ) || empty( $wishlist_id ) || $wishlist_id <= 0 || $func != '' )
	{
    		$wl_count = ps_wishlist::get_wishlist_count();
    		$wl_list = ps_wishlist::get_wishlists( '', $GLOBALS['keyword'], $orderby, $limitstart, $limit );
	
    		$result = show_wishlists( $wl_list, $wl_count, $limitstart, $limit );

	}
	else
	{
    		$wl = ps_wishlist::get_wishlist( $wishlist_id );

    		$result = show_wishlist( $wl, $limitstart, $limit );
	}
}
else
{
global $mainframe;
	$mainframe->redirect( URL . 'index.php', $GLOBALS['VM_LANG']->_('PHPSHOP_MOD_NO_AUTH') );
}


/* ----------- */


/**
 * Show a list of wishlists
 * @author Forrest
 * @param array $wl_list
 * @param int $num_rows
 * @param int $limitstart
 * @param int $limit
 * @return boolean -- successful or not
 */
function show_wishlists( &$wl_list, $num_rows, $limitstart, $limit )
{
	global $VM_LANG, $modulename, $sess, $ps_html;

	if ($wl_list !== false)
	{
		// Create the Page Navigation
		$pageNav = new vmPageNav( $num_rows, $limitstart, $limit );

		// Create the List Object with page navigation
		$listObj = new listFactory( $pageNav );

		// print out the search field and a list heading
		$listObj->writeSearchHeader($VM_LANG->_('PHPSHOP_WISHLIST_LIST_LBL'), IMAGEURL."ps_image/wishlist.png", $modulename, "list");

		// start the list table
		$listObj->startTable();

		// these are the columns in the table
		$columns = Array (
			"#" => "width=\"20\"", 
			"<input type=\"checkbox\" name=\"toggle\" value=\"\" onclick=\"checkAll(".$num_rows.")\" />" => "width=\"5%\"",
			'ID' => 'width="5%"',
			$VM_LANG->_('PHPSHOP_WISHLIST_TITLE') => 'width="40%"',
			$VM_LANG->_('PHPSHOP_WISHLIST_USER_LBL') => 'width="25%"',
			$VM_LANG->_('PHPSHOP_WISHLIST_CREATED_LBL') => 'width="20%"',
			$VM_LANG->_('E_REMOVE') => "width=\"5%\""
		);

		$listObj->writeTableHeader( $columns );

		$i = 0;
		foreach ($wl_list as $wl)
		{
			$listObj->newRow();
	
			// The row number
			$listObj->addCell( $pageNav->rowNumber( $i ) );
	
			// The Checkbox
			$listObj->addCell( vmCommonHTML::idBox( $i, $wl['wishlist_id'], false, "wishlist_id" ) );
	
			$listObj->addCell( $wl['wishlist_id'] );
	
			$url = $_SERVER['PHP_SELF'] . "?page=$modulename.list&wishlist_id=" . $wl['wishlist_id'];
			$listObj->addCell( '<a href="' . $sess->url( $url . '&limitstart=' . $limitstart ) . '">' . $wl['name'] . ' (' . $wl['item_count'] . ')</a><br />' );

			$user = ps_wishlist::get_user_info( $wl['user_id'] );
			$url = $_SERVER['PHP_SELF'] . "?pshop_mode=admin&page=admin.user_form&user_id=" . $wl['user_id'];
			$listObj->addCell( '<a href="' . $sess->url( $url ) . '">' . $user . '</a><br />' );

			$listObj->addCell( date( 'M d, Y', strtotime( $wl['created'] ) ) );
	
			$GLOBALS['page'] = $modulename . '.list';
			$listObj->addCell( $ps_html->deleteButton( "wishlist_id", $wl['wishlist_id'], "wishlistDelete", $limitstart ) );

			$i++;
		}

		$listObj->writeTable();

		$listObj->endTable();

		$listObj->writeFooter( $GLOBALS['keyword'] );

		return true;
	}

	return false;
}

/**
 * Show a wishlist -- and its items
 * @author Forrest
 * @param array $wl
 * @param int $limitstart
 * @param int $limit
 * @return boolean -- successful or not
 */
function show_wishlist( $wl, $limitstart, $limit )
{
	global $VM_LANG, $sess;

	if ($wl !== false)
	{
		$user = ps_wishlist::get_user_info( $wl['user_id'] );

		echo '<h2>' . $wl['name'] . '</h2>';
		echo '<p style="font-style:italic">' . $wl['comments'] . '</p>';
		echo '<p><b>' . $VM_LANG->_('PHPSHOP_WISHLIST_CREATED_LBL') . ':</b> ' . date( 'M d, Y', strtotime( $wl['created'] ) ) . '</p>';
		echo '<p><b>' . $VM_LANG->_('PHPSHOP_WISHLIST_MODIFIED_LBL') . ':</b> ' . date( 'M d, Y', strtotime( $wl['modified'] ) ) . '</p>';
		echo '<p><b>' . $VM_LANG->_('PHPSHOP_WISHLIST_USER_LBL') . ':</b> ' . $user . '</p>';
		echo '<p><b>' . $VM_LANG->_('PHPSHOP_WISHLIST_PUBLIC_LBL') . ':</b> ' . $wl['public'] . '</p>';

		// get this wishlist's items
		$wl_items = ps_wishlist::get_wishlist_items( $wl['wishlist_id'] );

		$wishlist_item_list = '';
		if (count( $wl_items ) > 0)
		{

			echo '<table class="adminlist">';
			echo '<thead>';
			echo '<th></th>';
			echo '<th>' . $VM_LANG->_('PHPSHOP_PRODUCT_NAME_TITLE') . '</th>';

			echo '<th>' . $VM_LANG->_('PHPSHOP_CART_QUANTITY') . '</th>';
			echo '</thead>';
			echo '<tbody>';

			foreach ($wl_items as $wl_item) 
			{
				echo '<tr>';
				echo '<td>' . $wl_item['product_thumb_image'] . '</td>';

				$url =	$_SERVER['PHP_SELF'] . "?page=product.product_form&product_id=" . $wl_item['product_id'];
				echo '<td><a href="' . $sess->url( $url ) . '">' . $wl_item['product_name'] . '</a></td>';


				echo '<td>' . $wl_item['product_quantity'] . '</td>';
				echo '</tr>';
			}

			echo '</tbody>';
			echo '</table>';
		}
		else
		{
			echo '<p>' . $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_NO_WISHLISTS_ITEMS') . '</p>';
		}

		return true;
	}

	return false;
}
?>
